package com.and.action;

import java.util.List;
import java.util.Set;

import javax.annotation.Resource;

import org.apache.struts2.ServletActionContext;
import org.springframework.stereotype.Controller;

import com.and.bean.Participle;
import com.and.bean.ParticipleCategory;
import com.and.bean.Suggestion;
import com.and.dao.Dao;
import com.and.dao.QueryResult;
@Controller
public class CategoryAction {
	
	@Resource
	protected Dao dao;
	
	private QueryResult<ParticipleCategory> cust;
	
	private Integer id;
	private Integer pid;
	private String category;
	private Set<Participle> participle;
	private Set<Suggestion> suggestion;
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	
	public Integer getPid() {
		return pid;
	}
	public void setPid(Integer pid) {
		this.pid = pid;
	}
	public String getCategory() {
		return category;
	}
	public void setCategory(String category) {
		this.category = category;
	}
	public Set<Participle> getParticiple() {
		return participle;
	}
	public void setParticiple(Set<Participle> participle) {
		this.participle = participle;
	}
	public Set<Suggestion> getSuggestion() {
		return suggestion;
	}
	public void setSuggestion(Set<Suggestion> suggestion) {
		this.suggestion = suggestion;
	}
	public String save() {
		ParticipleCategory mParticipleCategory = new ParticipleCategory();
		mParticipleCategory.setCategory(category);
		dao.save(mParticipleCategory);
		return "save";
	}
	public String findAllCategory(){
		cust= dao.getScrollData(ParticipleCategory.class);
		ServletActionContext.getRequest().setAttribute("orgs", cust);
		return "findAllCategory";
	}
	
	public String updatePC(){
		System.out.println("--------"+id+"---"+pid);
		Participle mParticiple = dao.find(Participle.class, pid);
		ParticipleCategory mParticipleCategory = dao.find(ParticipleCategory.class, id);
		mParticiple.setParticipleCategory(mParticipleCategory);
		dao.update(mParticiple);
		
		QueryResult<Suggestion> query = dao.getScrollData(
				Suggestion.class, "participle = ?1",
				new Object[] { mParticiple });
		List<Suggestion>list = query.getGetall();
		for (Suggestion suggestion : list) {
			suggestion.setParticipleCategory(mParticipleCategory);
			dao.update(suggestion);
		}
		return"updatePC";
	}
	
}
